import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { AlertController } from 'ionic-angular/components/alert/alert-controller';
import { Api } from '../../../providers/api-service';
import { cityData } from '../../../global/lib';

/**
 * 
 *personaldata 个人资料
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-personaldata',
  templateUrl: 'personaldata.html',
})
export class PersonaldataPage {

  constructor(
    public navCtrl: NavController,
    public alertCtrl: AlertController,
    public api: Api,
    public navParams: NavParams) {
  }

  userInfo = new PersonalData({
    HeadPortrait: "",
    NickName: "",
    Sex: false,
    Birthday: "",
    CityID: "",
    Blood: "",
    Constellation: "",
    Text: "",
    Height: "",
    Weight: ""
  });
  PersonalData;
  ionViewDidLoad() {
    console.log(' 个人资料 ionViewDidLoad PersonaldataPage');
    this.getUserInfo();
  }
  /**
   * 获取个人信息
   */
  getUserInfo() {
    this.api.postBody("V1/GetUserInfo", {})
      .then((data) => {
        console.log("个人信息")
        console.log(data);
        if (data.Code == 0) {
          var user = data.Data;
          this.userInfo = new PersonalData(user);
          console.log(this.userInfo);
        }
      })
  }
/**
 * 选择
 */
  changeDate(val) {
    console.log(val);
  }

  /**
   * 保存
   */
  save() {
    this.api.postBody("V1/UppdateUserInfo", this.userInfo)
      .then(data => {
        if (data.Code == 0) {
          this.api.showNoticeByToast(2, "修改成功");
          // setTimeout(function () {
          //   //this.navCtrl.pop();
          // }, 2000);
        }
      })
  }
  /**
   * 列表选择按钮
   * @param index 1、血型 
   */
  openPage(index) {
    let type = {};
    switch (index) {
      case 1: //血型
        type = {
          title: '血型',
          aray: ['A', 'B', 'O', 'AB']
        };
        break;
      case 2: //星座
        type = {
          aray: [
            '白羊座(3月21日-4月19日)',
            '金牛座(4月20日-5月20日)',
            '双子座(5月21日-6月21日)',
            '巨蟹座(6月22日-7月22日)',
            '狮子座(7月23日-8月22日)',
            '处女座(8月23日-9月22日)',
            '天秤座(9月23日-10月23日)',
            '天蝎座(10月24日-11月22日)',
            '射手座(11月23日-12月21日)',
            '摩羯座(12月22日-1月19日)',
            '水瓶座(1月20日-2月18日)',
            '双鱼座(2月19日-3月20日)'
          ],
          title: '星座'
        };
        break;
      case 3: //身高
        type = {
          title: '身高'
        };
        break;
      case 4: //体重
        type = {
          title: '体重'
        };
        break;
      case 5: //昵称
        type = {
          title: '昵称'
        };
        break;
    }
    this.showRadio(type, index);
  }

  showRadio(ays, key) {
    let alert = this.alertCtrl.create();
    if (key == 1 || key == 2) {
      alert.setTitle('选择' + ays.title);
      for (let index = 0; index < ays.aray.length; index++) {
        let item = ays.aray[index];
        alert.addInput({
          type: 'radio',
          label: item,
          value: item,
          checked: false
        });
      }
    } else {
      alert.setTitle('请填' + ays.title);
      let placeholderStr = "";
      if (key == 3) {
        placeholderStr = '请填写身高（单位:CM）';
      } else if(key == 4) {
        placeholderStr = '请填写体重（单位:Kg）';
      } else if (key == 5) {
        placeholderStr = '请填写昵称';
      }
      alert.addInput({
        type: 'txt',
        placeholder: placeholderStr
      });
    }
    alert.addButton('取消');
    alert.addButton({
      text: '确定',
      handler: (data) => {
        //console.log(JSON.stringify(data));
        switch (key) {
          case 1:
            this.userInfo.Blood = data + '型';
            break;
          case 2:
            this.userInfo.Constellation = data;
            break;
          case 3:
            this.userInfo.Height = data['0'] + 'CM';
            break;
          case 4:
            this.userInfo.Weight = data['0'] + 'Kg';
            break;
        }
      }
    });
    alert.present();
  }

  sex = "";
  /**
   * 性别
   * @param val 
   */
  showRadioSax(val) {
    let alert = this.alertCtrl.create();
    alert.setTitle('选择性别');
    alert.addInput({
      type: 'radio',
      label: '男',
      value: '男',
      checked: (val == "男")
    });
    alert.addInput({
      type: 'radio',
      label: '女',
      value: '女',
      checked: (val == '女')
    });
    alert.addButton('Cancel');
    alert.addButton({
      text: 'OK',
      handler: data => {
        this.sex = data;
        if (data == "男") {
          this.userInfo.Sex = true;
        } else {
          this.userInfo.Sex = false;
        }
      }
    });
    alert.present();
  }


  cityColumns = cityData;
  btn(key) {
    let str = "";
    switch (key) {
      case "NickName":
        str = "NickName  昵称";
        this.openPage(5);
        break;
      case "CityID":  //CityID
        str = "CityID  所在城市";
        this.openPage(7);
        break;
      case "Blood":
        str = "Blood  血型";
        this.openPage(1);
        break;
      case "Constellation":
        str = "Constellation  星座";
        this.openPage(2);
        break;
      case "Height":
        str = "Height  身高";
        this.openPage(3);
        break;
      case "Weight":
        this.openPage(4);
        str = "Weight  体重";
        break;
    }
    console.log(str);

  }
}

class PersonalData {
  constructor(public data) {
    this.Text = data['Text'] == null || data['Text'] == "" ? "" : data['Text'];
    this.Weight = this.set(data['Weight']);
    this.Height = this.set(data['Height']);
    this.Constellation = this.set(data['Constellation']);
    this.Blood = this.set(data['Blood']);
    this.CityID = this.set(data['CityID']);
    this.Birthday = this.setBirthbay(data['Birthday']);
    this.Sex = this.set(data['Sex']);
    this.NickName = this.set(data['NickName']);
    this.HeadPortrait = data['HeadPortrait'] == null || data['HeadPortrait'] == "" ? "" : data['HeadPortrait'];
  }
  HeadPortrait; //头像
  Text; //说明
  Weight;//体重
  Height;//身高
  Constellation;//星座
  Blood;//血型
  CityID;//城市
  Birthday;//出生年月
  Sex;//性别
  NickName;//昵称

  setBirthbay(data) {
    let str;
    if (data) {
      str = new Date(data * 1000).toISOString().substring(0, new Date().toISOString().indexOf("T"));
    } else {
      str = new Date().toISOString().substring(0, new Date().toISOString().indexOf("T"));
    }
    console.log(str);
    return str;
  }

  set(data) {
    if (data != null && data != "") {
      return data;
    }
    return "未设置";
  }
}
